package com.ruoyi.erp.transport.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.transport.domian.TransportUnitType;
import com.ruoyi.erp.transport.mapper.TransportUnitTypeMapper;
import com.ruoyi.erp.transport.service.TransportUnitTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TransportUnitTypeServiceImpl implements TransportUnitTypeService {

    private static final Logger log = LoggerFactory.getLogger(TransportUnitTypeServiceImpl.class);
    @Autowired
    TransportUnitTypeMapper transportUnitTypeMapper;

    @Override
    public List<TransportUnitType> selectTransportUnitTypeAll(String weightCapacity, String volumeCapacity, String transportUnitTypeId, String description, String uomForWeight, String uomForVolume) {
        return transportUnitTypeMapper.selectTransportUnitTypeAll(weightCapacity, volumeCapacity, transportUnitTypeId, description, uomForWeight, uomForVolume);
    }

    /**
     * 校验主键
     *
     * @param type 运输单位类型
     * @return 结果
     */
    @Override
    public String checkTransportCodeUnique(TransportUnitType type) {
        Integer delflag = type.getDelFlag()== null ? -1 : type.getDelFlag();
        String rowkey = type.getRowkey()== null ? "" : type.getRowkey();
        TransportUnitType info = transportUnitTypeMapper.checkTransportCodeUnique(type.getTransportUnitTypeId());
        if(StringUtils.isNull(info)){
            return UserConstants.UNIQUE;
        }else{
            if (rowkey.equals(info.getRowkey()))
            {
                return UserConstants.UNIQUE;
            }
        }
        return UserConstants.NOT_UNIQUE;
    }

    /**
     * 校验主键
     *
     * @param type 运输单位类型
     * @return 结果
     */
    @Override
    public String checkTransportCodeUniqueAdd(TransportUnitType type) {
        TransportUnitType info = transportUnitTypeMapper.checkTransportCodeUnique(type.getTransportUnitTypeId());
        if (StringUtils.isNull(info))
        {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }



    /**
     * 新增运输单位类型信息
     *
     * @param type 运输单位类型信息
     * @return 结果
     */
    @Override
    public int insertTransport(TransportUnitType type) {
        if(type.getDelFlag()==null||"".equals(type.getDelFlag())){
            type.setDelFlag(0);
        }
        return transportUnitTypeMapper.insertTransport(type);
    }
    /**
     * 修改运输单位类型信息
     *
     * @param type 修改运输单位类型信息
     * @return 结果
     */
    @Override
    public int updateTransport(TransportUnitType type) {
        return transportUnitTypeMapper.updateTransport(type);
    }

    /**
     * 新增或修改运输单位类型
     *
     * @param type 运输单位类型信息
     * @return 结果
     */
    @Override
    public int insertOrupdateTransport(TransportUnitType type) {
        TransportUnitType info = transportUnitTypeMapper.checkTransportCodeUnique(type.getTransportUnitTypeId());
        int addOrUpdate =0;
        if(info!=null){
            addOrUpdate = transportUnitTypeMapper.updateTransport(type);
            if(addOrUpdate == 1){
                addOrUpdate = 98;
            }
        }else{
            type.setDelFlag(0);
            addOrUpdate = transportUnitTypeMapper.insertTransport(type);
            if(addOrUpdate == 1){
                addOrUpdate = 99;
            }
        }
        return addOrUpdate;
    }

    @Override
    public String importTransport(List<TransportUnitType> typeList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(typeList) || typeList.size() == 0)
        {
            throw new CustomException("导入运输单位类型数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (TransportUnitType type : typeList)
        {
            try
            {
                // 验证是否存在这个运输单位类型
                TransportUnitType t = transportUnitTypeMapper.selectTransportByCode(type.getTransportUnitTypeId());
                if (StringUtils.isNull(t))
                {

                    type.setCreateBy(operName);
                    this.insertTransport(type);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、运输单位类型 " + type.getTransportUnitTypeId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    type.setUpdateBy(operName);
                    this.updateTransport(type);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、运输单位类型 " + type.getTransportUnitTypeId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、运输单位类型 " + type.getTransportUnitTypeId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、运输单位类型 " + type.getTransportUnitTypeId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<TransportUnitType> selectTransportUnitList(String ids) {
        return transportUnitTypeMapper.selectTransportUnitList(ids != null&&!"".equals(ids) ? Convert.toStrArray(ids) : null);
    }

    @Override
    public int deleteTransportByIds(String ids) {
        if(ids!=null&&!"".equals(ids)){
            return transportUnitTypeMapper.deleteTransportByIds(Convert.toStrArray(ids));
        }else{
            return -1;
        }
    }

    @Override
    public TransportUnitType selectTransportCode(String transportUnitTypeId) {
        return transportUnitTypeMapper.selectTransportByCode(transportUnitTypeId);
    }
}
